<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script>

        window.onload = function(){

            //输出5次，结果都为5
            /**
            for(var i=0;i<5;i++){
                setTimeout(function(){
                    console.log(i);
                },1000);
            }
             */

            //利用闭包原理
            for(var i=0;i<5;i++){
                (function(e){
                    setTimeout(function(){
                        console.log(e);
                    },1000);
                })(i);
            }
        }
    </script>
</head>
<body>

</body>
</html>